/**
 * Description:该类主要用于创建一个输入框
 * Copyright: 成都和为科技有限公司 2012
 * 
 * 属性：
 *   text - 输入框默认值
 *   placement - 弹出框显示位置
 */
function TimeInput() {}

TimeInput.inherit(Input);

/**
 * 初始化方法
 */
TimeInput.prototype.init = function () {
    TimeInput.base.init.call(this);
}

/**
 * 创建HTML节点
 */
TimeInput.prototype.create = function () {
    var template = "<div>" +
                         "<div class='input-group clockpicker' data-autoclose='true'>" +
                             "<input type='text' class='form-control'/>" +
                             "<span class='input-group-addon'><span class='glyphicon glyphicon-time'></span></span>" +
                        "</div>" +
                    "</div>";
    this._html = $(template)[0];
}


TimeInput.prototype.initLoad = function() {
    TimeInput.base.initLoad.call(this);
    if (!$().clockpicker) {
        this.loadScriptFile("/public.common/webapi/common/clockpicker/bootstrap-clockpicker.js");
        this.loadCSSFile("/public.common/webapi/common/clockpicker/bootstrap-clockpicker.css");
    }
    var oInput = $("input",this._html);
    if (this._attr.describle) {
        oInput.attr("placeholder", this._attr.describle);
    }
    if (this._attr.text) {
        oInput[0].value = this._attr.text;
    }
    if (this._attr.placement) {
        $(".clockpicker",this.getHTML()).data("placement", this._attr.placement);
    }
    if (this._attr.disabled == "true") {
        this.setDisable();
    }
    var othis = this;
    oInput.bind("focus", function(e){
		othis.focus(e);
	});
    oInput.bind("blur", function(e){
		othis.blur(e);
	});
	
    this.clockpicker = false;
    $(this.getHTML()).bind("click", function() {
        if (!othis.clockpicker) {
            $('.clockpicker', othis.getHTML()).clockpicker();
            $('.clockpicker', othis.getHTML()).on("change", function(e) {
                var event = new Object();
                event.currentTarget = $("input", othis.getHTML())[0];
                othis.setValidate(true);
                othis.blur(event);
            });
            $("input", othis.getHTML()).focus();
        }
        othis.clockpicker = true;
    });
}

/**
 * 自带认证方法。不能输入特殊符号
 * @param {} sValue
 * @return {Boolean}
 */
TimeInput.prototype.validateControl = function(sValue) {
	if (sValue == "") return;
    var reg = /^([0-1]?[0-9]|2[0-3]):([0-5]?[0-9])$/;
    if (sValue.match(reg) == null) {
        this.setValidate(false,"请录入正确格式(hh:ii)!");
    }
	return true;
}

/**
 * 获取当前控件的值。需要提交到数据库
 */
TimeInput.prototype.getValue = function(){
	if(!this.isValidate()){
		return "";
	}else{
		return $("input",this._html).val();
	}
}

/**
 * 设置当前控件的值。
 */
TimeInput.prototype.setValue = function(value){
    SuggestInput.base.setValue.call(this);
	$("input",this._html).val(value);
}

/**
 * 设置为可编辑
 */
TimeInput.prototype.setEnable = function(){
	TimeInput.base.setEnable.call(this);
	$("input",this._html)[0].disabled = false;
	$(".input-group-addon",this._html).show();
}

/**
 * 设置为只读
 */
TimeInput.prototype.setDisable = function(){
	TimeInput.base.setDisable.call(this);
	$("input",this._html).attr("disabled","true");
	$(".input-group-addon",this._html).hide();
}